cc.Class({
    extends: cc.Component,

    properties: {
        GM:null,
        Player:{
            default: null,
            type: cc.Node
        },
        Option: null,
        Grid: null,
        seed: null
    },

    SeededRandom:function(max, min){
        max = max || 1;
        min = min || 0;
        this.seed = (this.seed * 9301 + 49297) % 233280;
        var rnd = this.seed / 233280.0;
        return min + rnd * (max - min);
    },

    InstantiateMapGrid:function(){
        for(var j = this.Option.MapGridSize; j < this.Option.MapGridSize * 2;j++){
            for(var i = j - this.Option.MapGridSize +1; i < this.Option.MapGridSize * 2; i++){
                var newCell = cc.instantiate(this.Option.CellPrefab);
                this.InstantiateCell(i,j,newCell);
            }
        }
        for(var j = 1; j < this.Option.MapGridSize; j++){
            for(var i = 1; i < this.Option.MapGridSize + j; i++){
                var newCell = cc.instantiate(this.Option.CellPrefab);
                this.InstantiateCell(i,j,newCell);
            }
        }
    },    
    //生成细胞时的初始化
    InstantiateCell:function(i,j,newCell){            
        newCell.name = i + "," + j;             
        newCell.setPosition(cc.v2((this.Option.CellSize + this.Option.CellInterval) * Math.sqrt(3) * 0.5 * (i - this.Option.MapGridSize),(this.Option.CellSize + this.Option.CellInterval) * (j - this.Option.MapGridSize) - (this.Option.CellSize + this.Option.CellInterval) * 0.5 * (i - this.Option.MapGridSize)));                             
        this.Grid.MapGrid[i][j] = newCell;               
        newCell.getComponent('CellBase').MapX = i;               
        newCell.getComponent('CellBase').MapY = j;               
        newCell.parent = cc.find("Canvas/MapGrid");               
        newCell.setContentSize(this.Option.CellSize, this.Option.CellSize);
        newCell.getComponent('CellBase').Option = this.Option;
        newCell.getComponent('CellBase').Grid = this.Grid;
        newCell.getComponent('CellBase').GM = this.GM;
        newCell.getComponent('CellBase').Player = this.Player;
        newCell.getComponent('CellBase').Bacteria = this.Player.getComponent("Bacteria");
    },
    //全体细胞的初始化,每次重开游戏\地图重生使用
    InitiateCells(self){
        console.log("地图重置!");
        for(var j = self.Option.MapGridSize; j < self.Option.MapGridSize * 2;j++){
            for(var i = j - self.Option.MapGridSize +1; i < self.Option.MapGridSize * 2; i++){
                self.Grid.MapGrid[i][j].active = true;
                self.RadomCellType(i,j,self.Grid.MapGrid[i][j]);
            }
        }
        for(var j = 1; j < self.Option.MapGridSize; j++){
            for(var i = 1; i < self.Option.MapGridSize + j; i++){
                self.Grid.MapGrid[i][j].active = true;
                self.RadomCellType(i,j,self.Grid.MapGrid[i][j]);
            }
        }
    },
    // 细胞的类型随机并初始化
    RadomCellType(i,j,newCell){
        newCell.active =false;
        this.Grid.CellType[i][j] = Math.floor(this.SeededRandom(this.Option.Color.length / 2,0));
        newCell.getComponent('CellBase').type = this.Grid.CellType[i][j];
        newCell.getComponent('CellBase').ChangeColor();
        //console.log(Math.floor(this.SeededRandom(this.Option.Color.length,0)));
        //只显示给定范围内的细胞
        if(j >= this.Option.MapGridSize && j < this.Option.MapGridSize + this.Option.MapSize){
            if(i > j - this.Option.MapSize && i < this.Option.MapGridSize + this.Option.MapSize){
                newCell.active =true;
            }
            else{
                newCell.active = false;
            }
        }
        else if(j > this.Option.MapGridSize - this.Option.MapSize && j < this.Option.MapGridSize){
            if(i > this.Option.MapGridSize - this.Option.MapSize && i < j + this.Option.MapSize){
                newCell.active =true;
            }
            else{
                newCell.active = false;
            }
        }
        else{
            newCell.active = false;
        }
    },

    //对地图网格中非最外层细胞执行绑定邻居
    BindAdjCell:function (){
        for(var j = this.Option.MapGridSize; j < this.Option.MapGridSize * 2;j++){
            for(var i = j - this.Option.MapGridSize +1; i < this.Option.MapGridSize * 2; i++){
                this.Grid.MapGrid[i][j].getComponent("CellBase").BindAdjCell();
            }
        }
        for(var j = 1; j < this.Option.MapGridSize; j++){
            for(var i = 1; i < this.Option.MapGridSize + j; i++){
                this.Grid.MapGrid[i][j].getComponent("CellBase").BindAdjCell();
            }
        }     
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.GM = this.node.getComponent("GameController");
        this.Grid = this.GM.Grid;
        this.Option = this.GM.Option;
        this.seed = this.Option.seed;
    },

    start () {
        this.InstantiateMapGrid();        
        this.InitiateCells(this);
        this.BindAdjCell();

    },

    // update (dt) {},
});
